const passport = require("passport");

//引入jwtstractegy
const JwtStractegy=require('passport-jwt').Strategy,
ExtractJwt=require("passport-jwt").ExtractJwt;
const mongoose=require("mongoose");
const User=mongoose.model("users");
const keys=require("../config/keys");

const opts={}
opts.jwtFromRequest=ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey=keys.secretOrKey;


//处理passport
module.exports=passport=>{
    passport.use(new JwtStractegy(opts,(jw_payload,done)=>{
        console.log(jw_payload);
        User.findById(jw_payload.id)
            .then(user=>{
                if(user){
                    return done(null,user);
                }

                return done(null,false);
            })
            .catch(err=>console.log(err));
    }));
}